package stony.rl2Feathers.feathers 
{
	//
	import feathers.system.DeviceCapabilities;
	import feathers.themes.AeonDesktopTheme;
	import flash.display.Stage;
	import flash.display3D.Context3DProfile;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import starling.core.Starling;
	import starling.events.Event;
	//
	/**
	 * ...
	 * @author 
	 */
	//
	public class FeathersBase
	{
		private var _starling:Starling;
		private var _stage:Stage;
		
		public function FeathersBase(stage:Stage,starlingRootClass:Class) 
		{
			_stage = stage;
			
			//_stage.scaleMode = StageScaleMode.NO_SCALE;
			//_stage.align = StageAlign.TOP_LEFT;
			
			Starling.handleLostContext = true;
			//Starling.multitouchEnabled = true;
			
			DeviceCapabilities.dpi = 264;
			
			this._starling = new Starling(starlingRootClass, stage,null,null,"auto",Context3DProfile.BASELINE_EXTENDED);
			this._starling.antiAliasing = 0;
			this._starling.enableErrorChecking = false;
			this._starling.showStats = true;
			
			if (!this._starling.root) {
				this._starling.addEventListener(starling.events.Event.CONTEXT3D_CREATE, onStarlingContext3DCreated);
			}else {
				onStarlingContext3DCreated();
			}
			
			_stage.addEventListener(flash.events.Event.RESIZE, stageResizeHandler, false, int.MAX_VALUE, true);
			_stage.addEventListener(flash.events.Event.DEACTIVATE, stageDeactivateHandler, false, 0, true);
			_stage.addEventListener(flash.events.Event.ACTIVATE, stageActivateHandler, false, 0, true);
			
			stageActivateHandler();
        }
		
		private function onStarlingContext3DCreated(e:starling.events.Event=null):void 
		{
			new AeonDesktopTheme();
			//new MetalWorksDesktopTheme();
		}
		
		private function stageDeactivateHandler(event:flash.events.Event):void
		{
			this._starling.stop();
		}

		private function stageActivateHandler(event:flash.events.Event=null):void
		{
			this._starling.start();
		}
		
		private function stageResizeHandler(event:flash.events.Event):void
		{
			this._starling.stage.stageWidth = _stage.stageWidth;
			this._starling.stage.stageHeight = _stage.stageHeight;

			const viewPort:Rectangle = this._starling.viewPort;
			viewPort.width = _stage.stageWidth;
			viewPort.height = _stage.stageHeight;
			try
			{
				this._starling.viewPort = viewPort;
			}
			catch (error:Error) { }
			//this._starling.showStatsAt(HAlign.LEFT, VAlign.BOTTOM);
		}
		
		public function get starling():Starling 
		{
			return _starling;
		}
	}

}